<template>
  <div>
    <el-select placeholder="请选择" v-model="brandId" filterable clearable>
      <el-option
        v-for="item in brands"
        :key="item.brandId"
        :label="item.brandName"
        :value="item.brandId"
      ></el-option>
    </el-select>
  </div>
</template>

<script>
import PubSub from 'pubsub-js';

export default {
  components: {},
  props: {},
  data() {
    return {
      catId: 0,
      brands: [],
      brandId: "",
      subscribe: null
    };
  },
  watch: {
    brandId(val) {
      PubSub.publish("brandId", val);
      console.log("brandId",val);
      
    }
  },
  methods: {
    getCatBrands() {
      // this.$http({
      //   url: this.$http.adornUrl("/product/categorybrandrelation/catelog/list"),
      //   method: "get",
      //   params: this.$http.adornParams({
      //     brandId: this.brandId
      //   })
      // }).then(({ data }) => {
      //   this.cateRelationTableData = data.data.list;
      // });
      this.$http({
        url: this.$http.adornUrl("/product/categorybrandrelation/brands/list"),
        method: "get",
        params: this.$http.adornParams({
          catId: this.catId
        })
      }).then(({ data }) => {
        if (data && data.code==200) {

          console.log("data  品牌信息",data);
        // 过滤掉 null 或 undefined 后再进行映射
        this.brands = data.data.filter(item => item).map(item => ({
          brandId: item.brand_id,
          brandName: item.brandName
        }));
          
        } else {
          console.error('Unexpected response format:', data);
          this.brands = [];
        }
      });
    }
  },
  created() {},
  mounted() {
    this.subscribe = PubSub.subscribe("catPath", (msg, val) => {
      this.catId = val[val.length - 1];
      this.getCatBrands();
    });
  },
  beforeDestroy() {
    PubSub.unsubscribe(this.subscribe); //销毁订阅
  }
};
</script>

<style scoped>
/* 样式部分 */
</style>